package com.ln.第三遍1105.demo04;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Ln
 * @version 终极版
 * @date 2022/11/6 16:41
 */
public class Test01 {
    public static void main(String[] args) {
        List<Person> personList = new ArrayList<>();
        personList.add(new Person("Tom",24,"美国",'M'));
        personList.add(new Person("欧阳雪",18,"中国",'F'));
        personList.add(new Person("Harley",22,"英国",'F'));
        personList.add(new Person("小梅",20,"中国",'F'));
        personList.add(new Person("向天笑",20,"中国",'M'));
        personList.add(new Person("李康",22,"中国",'M'));
        personList.add(new Person("何雪",21,"中国",'F'));

        //collect搜集操作
        //计数
        Long collect1 = personList.stream().collect(Collectors.counting());
        System.out.println(collect1);
        //平均值
        Double collect2 = personList.stream().collect(Collectors.averagingInt(t -> t.getAge()));
        System.out.println(collect2);
        //最大值
        Optional<Person> collect3 = personList.stream().collect(Collectors.maxBy((o1, o2) -> o1.getAge() - o2.getAge()));
        System.out.println(collect3.get());
        //求和
        Integer collect4 = personList.stream().collect(Collectors.summingInt(t -> t.getAge()));
        System.out.println(collect4);
        //搜集为一个新的集合。
        Map<String, Person> map = personList.stream().filter(t -> t.getAge() > 20)
                .filter(t -> t.getCountry().equals("中国"))
                .collect(Collectors.toMap(t -> t.getName(), t -> t));
        System.out.println(map);

        Set<Person> set = personList.stream().filter(t -> t.getAge() > 20)
                .filter(t -> t.getCountry().equals("中国"))
                .collect(Collectors.toSet());
        System.out.println(set);

        //分组
        Map<String, List<Person>> collect = personList.stream().collect(Collectors.groupingBy(t -> t.getCountry()));
        System.out.println(collect);
    }
}
class Person {
    private String name;
    private Integer age;
    private String country;
    private char sex;

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", country='" + country + '\'' +
                ", sex=" + sex +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }

    public Person(String name, Integer age, String country, char sex) {
        this.name = name;
        this.age = age;
        this.country = country;
        this.sex = sex;
    }
}
